{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/konolabs/anaconda3/envs/tensorflow/lib/python3.6/site-packages/nltk/translate/bleu_score.py:490: UserWarning: \n",
      "Corpus/Sentence contains 0 counts of 2-gram overlaps.\n",
      "BLEU scores might be undesirable; use SmoothingFunction().\n",
      "  warnings.warn(_msg)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0.816496580927726"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import nltk\n",
    "\n",
    "hypothesis = [[\"open\", \"the\", \"file\"]\n",
    "reference = [\"open\", \"file\"]\n",
    "#the maximum is bigram, so assign the weight into 2 half.\n",
    "BLEUscore = nltk.translate.bleu_score.sentence_bleu([reference], hypothesis, weights = (0.5, 0.5))\n",
    "BLEUscore"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/konolabs/anaconda3/envs/tensorflow/lib/python3.6/site-packages/nltk/translate/bleu_score.py:490: UserWarning: \n",
      "Corpus/Sentence contains 0 counts of 2-gram overlaps.\n",
      "BLEU scores might be undesirable; use SmoothingFunction().\n",
      "  warnings.warn(_msg)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0.816496580927726"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "hypothesis = [\"1\", \"2\", \"3\"]\n",
    "reference = [[\"1\", \"3\"]]\n",
    "#the maximum is bigram, so assign the weight into 2 half.\n",
    "BLEUscore = nltk.translate.bleu_score.sentence_bleu(reference, hypothesis, weights = (0.5, 0.5))\n",
    "BLEUscore"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/konolabs/anaconda3/envs/tensorflow/lib/python3.6/site-packages/nltk/translate/bleu_score.py:490: UserWarning: \n",
      "Corpus/Sentence contains 0 counts of 4-gram overlaps.\n",
      "BLEU scores might be undesirable; use SmoothingFunction().\n",
      "  warnings.warn(_msg)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "1.0"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "hypothesis = [\"1\", \"2\", \"3\"]\n",
    "reference = [[\"1\", \"2\", \"3\"]]\n",
    "#the maximum is bigram, so assign the weight into 2 half.\n",
    "BLEUscore = nltk.translate.bleu_score.sentence_bleu(reference, hypothesis)\n",
    "BLEUscore"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "35.46279694045048"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "target1 = [[\"1\", \"2\", \"3\"]]\n",
    "predict1 = [\"1\", \"3\"]\n",
    "\n",
    "target2 = [[\"1\", \"2\", \"3\"]]\n",
    "predict2 = [\"1\", \"2\"]\n",
    "\n",
    "target3 = [[\"1\", \"2\", \"3\", \"4\", \"6\"]]\n",
    "predict3 = [\"1\", \"3\", \"4\", \"6\"]\n",
    "\n",
    "target4 = [[\"1\", \"2\", \"3\", \"8\", \"10\"]]\n",
    "predict4 = [\"1\", \"2\", \"3\", \"8\"]\n",
    "\n",
    "target5 = [[\"1\", \"3\", \"2\", \"5\"]]\n",
    "predict5 = [\"1\", \"2\", \"5\"]\n",
    "\n",
    "targets = [target1, target2, target3, target4, target5]\n",
    "predicts = [predict1, predict2, predict3, predict4, predict5]\n",
    "\n",
    "result = nltk.translate.bleu_score.corpus_bleu(targets, predicts)\n",
    "result * 100"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.6 (NLP)",
   "language": "python",
   "name": "nlp"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
